using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace EscapeFromLenovo
{
    /// <summary>
    /// Handles all sprites called in the game, checks collision detection
    /// </summary>
    public class SpriteManager : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        List<AutoSprite> spriteList = new List<AutoSprite>();  //List of AutoSprites to contain enemies   
        List<AutoSprite> livesList = new List<AutoSprite>(); //List of Bill images to depict remaining lives
        Player player;  //Player declaration        

        //CONSTRUCTOR: SpriteManager is a drawable game component, takes the Game as its parameter
        public SpriteManager(Game game)
            : base(game)
        {}

        //INITIALIZE METHOD
        public override void Initialize()
        {
            base.Initialize();
        }

        //LOAD CONTENT: instantiates a player and as many enemies as desired
        protected override void  LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
         
            //Bill
            player = new Player(Game.Content.Load<Texture2D>(@"Images\bill"), new Vector2(0, Game.Window.ClientBounds.Y+449), new Point(130, 270), 50, new Point(0, 0), new Point(8, 3), 120);
        

            //Skull
            spriteList.Add(new AutoSprite(Game.Content.Load<Texture2D>(@"Images\skullball"), new Vector2(100,Game.Window.ClientBounds.Y+600), new Point(75, 75), 10, new Point(0, 0), new Point(6, 8), 50, new Vector2(6, 6), 1f));

            //Life Images
            for(int i=0; i< ((Game1)Game).numberlives; ++i)
            {
                int offset = 10 + i * 45;
                livesList.Add(new AutoSprite(Game.Content.Load<Texture2D>(@"Images\bill"), new Vector2(offset, 65), new Point(130, 270), 10, new Point(0,0), new Point(8, 3), 0, Vector2.Zero, .25f));
            }
        }

        //COLLISION DETECTION: returns a boolean value
        public bool Collide()
        {
            foreach (AutoSprite s in spriteList)
            {
                if (player.collisionRect.Intersects(s.collisionRect))
                    return true;
            }
            
            return false;
        }

        //public bool blockCollide()

        //UPDATE METHOD: calls player's update, then calls enemy update "for each" one instantiated in the List, checks for collision
        public override void Update(GameTime gameTime)
        {
            player.Update(gameTime, Game.Window.ClientBounds);

            foreach (AutoSprite s in spriteList)
            {
                s.Update(gameTime, Game.Window.ClientBounds);
                if(Collide())
                {
                    GamePad.SetVibration(PlayerIndex.One, 1f, 1f);
                    GamePad.SetVibration(PlayerIndex.One, 0, 0);
                    player.position.X = 0;
                    //TEST//For coins at some point:((Game1)Game).addtoScore(s.scoreValue);   
                    if (livesList.Count > 0)
                    {
                        livesList.RemoveAt(livesList.Count - 1);
                        --((Game1)Game).numberlives;
                    }
                }                
            }
            foreach (AutoSprite x in livesList)
            {
                x.lifeUpdate();
            }

            base.Update(gameTime);
        }

        //DRAW METHOD: calls the player's draw, then calls the enemy draw "for each" one instantiated in the list
        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.FrontToBack, SaveStateMode.None);

            //Draw Bill
            player.Draw(gameTime, spriteBatch);

            //Draw spriteList
            foreach (AutoSprite s in spriteList)
                s.Draw(gameTime, spriteBatch);
            
            //Draw livesList
            foreach (AutoSprite x in livesList)
                x.Draw(gameTime, spriteBatch);

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}